package controlleur;

import entities.questionnaire.Questionnaire;
import controlleur.util.JsfUtil;
import controlleur.util.PaginationHelper;
import facade.AdministrationFacadeLocal;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

/**
 * managed bean traitant les entités de questionnaires
 * @author mickael.palanchon
 */
@ManagedBean (name="questionnaireDispoController")
@SessionScoped
public class QuestionnaireDispoController implements Serializable {

    private Questionnaire current;
    private DataModel items = null;
    @EJB
    private AdministrationFacadeLocal ejbFacade;

    @ManagedProperty(value="#{questionnaireController}")
    private QuestionnaireController questionnaireController;

    private PaginationHelper pagination;
   // private int selectedItemIndex;

    public QuestionnaireDispoController() {
    }

    public Questionnaire getSelected() {
        if (current == null) {
            current = new Questionnaire();
        }
        return current;
    }

    private AdministrationFacadeLocal getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().findAllQuestionnaireDispo(getFacade().findCreateur("loic.vaugeois")).size();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findQuestionnaireDispoRange(getFacade().findCreateur("loic.vaugeois"),new int[]{getPageFirstItem(), getPageFirstItem()+getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareView() {
        current = (Questionnaire)getItems().getRowData();
   //     selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

 


    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        return "List";
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAllQuestionnaire(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAllQuestionnaire(), true);
    }

    public Date getDateCloture() {
        return current.getDateCloture().getTime();
    }



    public void setDateDiffusion(Date dateDiffusion) {
        Calendar c = new GregorianCalendar();
        c.setTime(dateDiffusion);
        current.setDateDiffusion(c);
    }


    public String openQuestionnaire(){
        current = (Questionnaire)getItems().getRowData();
        questionnaireController.setSelected(current);
        return "ToQuestionnairePasser";
    }

    public QuestionnaireController getQuestionnaireController() {
        return questionnaireController;
    }

    public void setQuestionnaireController(QuestionnaireController questionnaireController) {
        this.questionnaireController = questionnaireController;
    }



    @FacesConverter(forClass=Questionnaire.class)
    public static class QuestionnaireControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            QuestionnaireDispoController controller = (QuestionnaireDispoController)facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "questionnaireController");
            return controller.ejbFacade.findQuestionnaire(getKey(value));
        }

        long getKey(String value) {
            long key;
            key = Long.parseLong(value);
            return key;
        }

        String getStringKey(long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Questionnaire) {
                Questionnaire o = (Questionnaire) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+QuestionnaireDispoController.class.getName());
            }
        }

    }    

}
